package com.gyms.manager;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.classic.okhttp.beans.HVProjectBean;
import com.classic.okhttp.beans.HVVenueMapBean;
import com.gyms.R;
import com.gyms.bean.HVVenueMyMapBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import utils.StringUtils;

/**
 * Created by ltx on 17/4/5.
 * 地图相关的管理类
 */

public class MapUtilsManger {
    private static MapUtilsManger instance;
    public ArrayList<HVVenueMyMapBean> mHVVenueMyMapBeans;
    private static final int sMaxDis=5000;//在这距离范围内显示标记
    public List<HVProjectBean> mHVProjectBeans;//运动分类list

    private Map<String,Integer> mMarkInts;//存放Icon
    public Map<String,String> mMpProject;//存放项目名称的Mp

    public static MapUtilsManger getInstance() {
        if (instance == null) {
            instance = new MapUtilsManger();
        }
        return instance;
    }

    private MapUtilsManger() {
        mHVVenueMyMapBeans=new ArrayList<>();
        mMarkInts=new HashMap<>();
        mMpProject=new HashMap<>();
        initMarkInts();
    }

    public void initProjectMp(List<HVProjectBean> mProjectBeans){
           mMpProject.clear();
        for (HVProjectBean mProjectBean:mProjectBeans){
            mMpProject.put(mProjectBean.getProjectId(),mProjectBean.getProjectName());
        }
    }

    /**
     * 初始化地图Marks
     */

    private void initMarkInts() {
        mMarkInts.put("综合", R.mipmap.hl_map_zonghe);
        mMarkInts.put("篮球", R.mipmap.hl_map_lanqiu);
        mMarkInts.put("射箭", R.mipmap.hl_map_shejian);
        mMarkInts.put("网球", R.mipmap.hl_map_wangqiu);
        mMarkInts.put("舞蹈", R.mipmap.hl_map_wudao);
        mMarkInts.put("游泳", R.mipmap.hl_map_youyong);
        mMarkInts.put("羽毛球", R.mipmap.hl_map_yumaoqiu);

        mMarkInts.put("橄榄球", R.mipmap.hl_map_ganlanqiu);
        mMarkInts.put("高尔夫", R.mipmap.hl_map_gaoerfu);
        mMarkInts.put("击剑", R.mipmap.hl_map_jijian);
        mMarkInts.put("健身", R.mipmap.hl_map_jianshen);

        mMarkInts.put("溜冰", R.mipmap.hl_map_liubing);
        mMarkInts.put("培训", R.mipmap.hl_map_peixun);
        mMarkInts.put("乒乓球", R.mipmap.hl_map_pingpangqiu);
        mMarkInts.put("台球", R.mipmap.hl_map_taiqiu);
        mMarkInts.put("武术", R.mipmap.hl_map_wushu);
        mMarkInts.put("瑜伽", R.mipmap.hl_map_yujia);

        mMarkInts.put("户外", R.mipmap.hl_map_huwai);
        mMarkInts.put("少儿", R.mipmap.hl_map_shaoer);
        mMarkInts.put("棋牌", R.mipmap.hl_map_qipai);
        mMarkInts.put("全部", R.mipmap.hl_map_quanbu);
        mMarkInts.put("足球", R.mipmap.hl_map_zuqiu);
    }

    /**
     * 初始化MyMapBeans 设置icon
     * @param mHVVenueMapBeans 地图相关bean
     * @param mProjectId 临时设置的项目ID
     */
    public void setHVVenueMyMapBeans( ArrayList<HVVenueMapBean> mHVVenueMapBeans,String mProjectId){
        for (HVVenueMapBean mMapBean:mHVVenueMapBeans){
            HVVenueMyMapBean mMyMapBean=new HVVenueMyMapBean();
            mMyMapBean.mHVVenueMapBean=mMapBean;
            mMyMapBean.setMarkIcon(getMarkIcon(mProjectId));
            mHVVenueMyMapBeans.add(mMyMapBean);
        }
    }

    /**
     * 获取与当前的经纬度之间的距离小于5公里之内的点
     * @param mCurLatLng 当前屏幕中心的经纬度
     */

    public void getCurDIstanceBeans(LatLng mCurLatLng){

        for (HVVenueMyMapBean mCUrMapBean:mHVVenueMyMapBeans
             ) {
            LatLng mTempLatLng=new LatLng(mCUrMapBean.mHVVenueMapBean.getLatitude(),mCUrMapBean.mHVVenueMapBean.getLongitude());
            float mTempLineDis=AMapUtils.calculateLineDistance(mCurLatLng,mTempLatLng);//当前的距离
            if(mTempLineDis<=sMaxDis){//取在五公里之内的点
                mCUrMapBean.setCanAdd(true);
            }
            else
            {
                mCUrMapBean.setCanAdd(false);
            }
        }

    }

    /**
     * 根据项目Id获取 相应的icon
     * @param mProjectId 项目Id
     * @return 对应的项目Icon
     */

    public int getMarkIcon(String mProjectId) {
        int mMarkIcon ;
        if(StringUtils.isNull(mProjectId)){//如果projectId为空就为null
            mMarkIcon=mMarkInts.get("全部");
            return mMarkIcon;
        }
        try {
            String mProjectName = mMpProject.get(mProjectId);
            mMarkIcon=mMarkInts.get(mProjectName);

        }catch (NullPointerException e){
            return mMarkInts.get("全部");
        }
        return mMarkIcon;
    }

}
